package com.lvmama.pangolin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.entity.InputCommodityVO;
import com.lvmama.pangolin.entity.hotel.HotelCommodityDetail;
import com.lvmama.pangolin.entity.hotel.HotelProductDetail;
import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.hotel.service.HotelCommodityDetailService;
import com.lvmama.pangolin.hotel.service.HotelProductDetailService;
import com.lvmama.pangolin.mt.service.MTTicketCommodityItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;

@Controller
@RequestMapping("/datashow")
public class InputCommodityController {

	@Autowired
	private XCTicketCommodityItemsService xcCommodityService;
	@Autowired
	private TNTicketCommodityItemsService tnCommodityService;
	@Autowired
	private TCCommodityItemsService tcCommodityService;
	@Autowired
	private QNRTicketCommodityItemsService qnrCommodityService;
	@Autowired
	private MTTicketCommodityItemsService mtCommodityService;
	@Autowired
	private HotelCommodityDetailService hotelCommService;
	@Autowired
	private HotelProductDetailService hotelProdService;
	@RequestMapping("/inputCommodity")
	public String showCommodityList(ModelMap model,String prodPageNum,InputCommodityVO co,String businessCode) {
		String productId=co.getProductId();
		String companyCode=co.getCompanyCode();
		if(businessCode==null||"".equals(businessCode)||"TICKET".equals(businessCode)){
			if (productId != null && !"".equals(productId)) {
				if (companyCode != null && "xc".equals(companyCode)) {
					List<XCTicketCommodityItem> xcComm = xcCommodityService.selectByCommVO(co);
					PageInfo<XCTicketCommodityItem>page=new PageInfo<XCTicketCommodityItem>(xcComm);
					model.addAttribute("page", page);
					model.addAttribute("commList", xcComm);
				} else if (companyCode != null && "tn".equals(companyCode)) {
					List<TNTicketCommodityItem> tnComm = tnCommodityService.selectByCommVO(co);
					PageInfo<TNTicketCommodityItem>page=new PageInfo<TNTicketCommodityItem>(tnComm);
					model.addAttribute("commList", tnComm);
					model.addAttribute("page", page);
				} else if (companyCode != null && "tc".equals(companyCode)) {
					List<TCommodityItem> tcComm = tcCommodityService.selectByCommVO(co);
					PageInfo<TCommodityItem>page=new PageInfo<TCommodityItem>(tcComm);
					model.addAttribute("commList", tcComm);
					model.addAttribute("page", page);
				} else if (companyCode != null && "qnr".equals(companyCode)) {
					List<QNRTicketCommodityItem> qnrComm = qnrCommodityService.selectByCommVO(co);
					PageInfo<QNRTicketCommodityItem>page=new PageInfo<QNRTicketCommodityItem>(qnrComm);
					model.addAttribute("commList", qnrComm);
					model.addAttribute("page", page);
				} else if (companyCode != null && "mt".equals(companyCode)) {
					List<MTTicketCommodityItem> qnrComm = mtCommodityService.selectByCommVO(co);
					PageInfo<MTTicketCommodityItem>page=new PageInfo<MTTicketCommodityItem>(qnrComm);
					model.addAttribute("commList", qnrComm);
					model.addAttribute("page", page);
				}
			}
		}else if("HOTEL".equals(businessCode)){
			co.setCompanyCode(companyCode.toUpperCase()+"_C_CODE");
			List<HotelCommodityDetail> commList=hotelCommService.findByInputCommodityVO(co);
			PageInfo<HotelCommodityDetail>page=new PageInfo<HotelCommodityDetail>(commList);
			model.addAttribute("commList", commList);
			model.addAttribute("page", page);
		}
		
		model.addAttribute("prodPageNum", prodPageNum);
		model.addAttribute("companyCode", companyCode);
		model.addAttribute("inputCommodityVO",co);
		model.addAttribute("businessCode",businessCode);
		return "/datashow/inputCommodity";
	}
	
	@RequestMapping(value = "/updateInputCommodity", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> updateInputCommodity(String productId,String companyCode,String id,String commodityId,
			String commodityName,String commodityType,String saleCond,String refundCond,String rackRate,
			String salePrice,String preferential,String taskId,String newGrabTime,String businessCode){
		Map<String, Object> result=getResult();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(businessCode==null||"".equals(businessCode)||"TICKET".equals(businessCode)){
			if(companyCode!=null&&"xc".equals(companyCode)){
				XCTicketCommodityItem item=new XCTicketCommodityItem();
				item.setProduct_id(productId);
				item.setCommodity_id(commodityId);
				item.setCommodity_name(commodityName);
				item.setCommodity_type(commodityType);
				item.setSale_cond(saleCond);
				item.setRefund_cond(refundCond);
				item.setRack_rate(rackRate);
				item.setSale_price(salePrice);
				item.setPreferential(preferential);
				try {
					item.setNew_grab_time(sdf.parse(newGrabTime));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				item.setTask_id(taskId);
				if(id!=null&&!"".equals(id)){
					item.setId(id);
					xcCommodityService.updateSelective(item);
					result.put("result", 1);
				}else{
					try {
						item.setCreate_time(sdf.parse(newGrabTime));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					id=RowkeyGenerator.generateRowkey(productId, new Date(), taskId);
					item.setId(id);
					xcCommodityService.insertSelective(item);
					result.put("result",0);
					result.put("cid",id);
				}	
				
			}else if(companyCode!=null&&"tn".equals(companyCode)){
				TNTicketCommodityItem item=new TNTicketCommodityItem();
				item.setProduct_id(productId);
				item.setCommodity_id(commodityId);
				item.setCommodity_name(commodityName);
				item.setCommodity_type(commodityName);
				item.setSale_cond(saleCond);
				item.setRefund_cond(refundCond);
				item.setRack_rate(rackRate);
				item.setSale_price(salePrice);
				item.setPreferential(preferential);
				item.setCreate_time(new Date());
				item.setNew_grab_time(new Date());
				item.setTask_id(taskId);
				if(id!=null&&!"".equals(id)){
					item.setId(id);
					tnCommodityService.updateSelective(item);
					result.put("result", 1);
				}else{
					id=RowkeyGenerator.generateRowkey(productId, new Date(), taskId);
					item.setId(id);
					tnCommodityService.insertSelective(item);
					result.put("result",0);
					result.put("cid",id);
				}	
			}else if(companyCode!=null&&"tc".equals(companyCode)){
				TCommodityItem item=new TCommodityItem();
				item.setProduct_id(productId);
				item.setCommodity_id(commodityId);
				item.setCommodity_name(commodityName);
				item.setCommodity_type(commodityName);
				item.setSale_cond(saleCond);
				item.setRefund_cond(refundCond);
				item.setRack_rate(rackRate);
				item.setSale_price(salePrice);
				item.setPreferential(preferential);
				item.setCreate_time(new Date());
				item.setNew_grab_time(new Date());
				item.setTask_id(taskId);
				if(id!=null&&!"".equals(id)){
					item.setId(id);
					tcCommodityService.updateSelective(item);
					result.put("result", 1);
				}else{
					id=RowkeyGenerator.generateRowkey(productId, new Date(), taskId);
					item.setId(id);
					tcCommodityService.insertSelective(item);
					result.put("result",0);
					result.put("cid",id);
				}	
			}else if(companyCode!=null&&"qnr".equals(companyCode)){
				QNRTicketCommodityItem item=new QNRTicketCommodityItem();
				item.setProduct_id(productId);
				item.setCommodity_id(commodityId);
				item.setCommodity_name(commodityName);
				item.setCommodity_type(commodityName);
				item.setSale_cond(saleCond);
				item.setRefund_cond(refundCond);
				item.setRack_rate(rackRate);
				item.setSale_price(salePrice);
				item.setPreferential(preferential);
				item.setCreate_time(new Date());
				item.setNew_grab_time(new Date());
				item.setTask_id(taskId);
				if(id!=null&&!"".equals(id)){
					item.setId(id);
					qnrCommodityService.updateSelective(item);
					result.put("result", 1);
				}else{
					id=RowkeyGenerator.generateRowkey(productId, new Date(), taskId);
					item.setId(id);
					qnrCommodityService.insertSelective(item);
					result.put("result",0);
					result.put("cid",id);
				}
			}else if(companyCode!=null&&"mt".equals(companyCode)){
				MTTicketCommodityItem item=new MTTicketCommodityItem();
				item.setProduct_id(productId);
				item.setCommodity_id(commodityId);
				item.setCommodity_name(commodityName);
				item.setCommodity_type(commodityName);
				item.setSale_cond(saleCond);
				item.setRefund_cond(refundCond);
				item.setRack_rate(rackRate);
				item.setSale_price(salePrice);
				item.setPreferential(preferential);
				item.setCreate_time(new Date());
				item.setNew_grab_time(new Date());
				item.setTask_id(taskId);
				if(id!=null&&!"".equals(id)){
					item.setId(id);
					mtCommodityService.updateSelective(item);
					result.put("result", 1);
				}else{
					id=RowkeyGenerator.generateRowkey(productId, new Date(), taskId);
					item.setId(id);
					mtCommodityService.insertSelective(item);
					result.put("result",0);
					result.put("cid",id);
				}
			}
		}else if("HOTEL".equals(businessCode)){
			HotelProductDetail hotelProd=hotelProdService.findByProductId(productId, companyCode.toUpperCase()+"_C_CODE");
			HotelCommodityDetail hotelComm=new HotelCommodityDetail();
			hotelComm.setProductId(productId);
			hotelComm.setCommodityId(commodityId);
			hotelComm.setCommodityName(commodityName);
//			hotelComm.setRoomType(commodityName);
			hotelComm.setCommodityType(commodityName);
			hotelComm.setCancelRule(refundCond);
			hotelComm.setSalePrice(salePrice);
			hotelComm.setPreferential(preferential);
			hotelComm.setCreateTime(new Date());
			hotelComm.setUpdateTime(new Date());
			hotelComm.setTaskId(taskId);
			hotelComm.setTaskDate(new Date());
			hotelComm.setCityCode(hotelProd.getCityCode());
			if(id!=null&&!"".equals(id)){
				hotelComm.setId(id);
				hotelCommService.update(hotelComm);
				result.put("result", 1);
			}else{
				id=RowkeyGenerator.generateProductRowkeyWithCompany(productId, companyCode.toUpperCase()+"_C_CODE", new Date(), taskId);
				hotelComm.setId(id);
				hotelComm.setCompanyCode(companyCode.toUpperCase()+"_C_CODE");
				hotelCommService.save(hotelComm);
				
				result.put("result",0);
				result.put("cid",id);
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "/deleteInputCommodity", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void deleteCareProduct(String id, String companyCode,String businessCode) {
		if(businessCode==null||"".equals(businessCode)||"TICKET".equals(businessCode)){
			if (companyCode != null && "xc".equals(companyCode)) {
				xcCommodityService.deleteById(id);
			} else if (companyCode != null && "tn".equals(companyCode)) {
				tnCommodityService.deleteById(id);
			} else if (companyCode != null && "tc".equals(companyCode)) {
				tcCommodityService.deleteById(id);
			} else if (companyCode != null && "qnr".equals(companyCode)) {
				qnrCommodityService.deleteById(id);
			} else if (companyCode != null && "mt".equals(companyCode)) {
				mtCommodityService.deleteById(id);
			}
		}else if("HOTEL".equals(businessCode)){
			hotelCommService.deleteById(id);
		}
		
	}
	public Map<String, Object> getResult() {
    	
        Map<String, Object> result = new HashMap<String, Object>();
        
        return result;
    }
}
